// JointPoint.cpp: implementation of the JointPoint class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "JointPoint.h"
#include "UEEntityOperation.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

JointPoint::JointPoint()
{
	m_pSHPt = NULL;
}

JointPoint::JointPoint(ModelObject *pObject)
{
	ASSERT(pObject);
	Model(pObject);
	m_pSHPt = NULL;
}

JointPoint::~JointPoint()
{

}

void JointPoint::Erase()
{
	Joint *pJoint = static_cast<Joint *>(Model());
	ASSERT(pJoint);

	Joint::Iterator* pIt = pJoint->NewAttachIterator();
	for( pIt->Begin(); !pIt->End(); pIt->Next() )
	{
		Part *pPart = pIt->Get();
		ASSERT(pPart);
		PartPoint pt = FindObserver(pPart);
		pt.Erase();
		RemoveSubDictionary(ltos(pPart->PartIndex()), m_DicHandle);
	}
	delete pIt;
}

void JointPoint::UnErase()
{

}

void JointPoint::SetDicHandle(const Dictionary& handle)
{
	m_DicHandle = handle;	
}

void JointPoint::Update(BOOL bUpdate)
{
	if(bUpdate)
	{
	}
	else
	{
		UpdateObserver();
	}
}

void JointPoint::UpdateObserver()
{
	Joint *pJoint = static_cast<Joint *>(Model());
	ASSERT(pJoint);

	Joint::Iterator* pIt = pJoint->NewAttachIterator();
	for( pIt->Begin(); !pIt->End(); pIt->Next() )
	{
		Part *pPart = pIt->Get();
		ASSERT(pPart);
		PartPoint pt = FindObserver(pPart);
		pt.Update();
	}
	delete pIt;
}

PartPoint JointPoint::FindObserver(Part* pPart)
{
	PartPoint pPt;
	
	CString strKey;
	strKey.Format(_T("%d"), pPart->PartIndex());
	pPt.Name(strKey);
	pPt.SetDicHandle(FindObserverDictionary(strKey));
	pPt.SHObserver(m_pSHPt);
	pPt.Model(pPart);

	return pPt;
}